package com.keson.spider;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttClientUtil {
	private static MqttClient sampleClient;
	private static int i = 1;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

	public static MqttClient getInstance() {
		if(sampleClient==null) {
			try {
				sampleClient = new MqttClient("tcp://47.94.80.3:61713", "HBserver", new MemoryPersistence());
				final MqttConnectOptions connOpts = new MqttConnectOptions();
				connOpts.setUserName("admin");
				connOpts.setPassword("keson-123".toCharArray());
				connOpts.setCleanSession(true);
				connOpts.setAutomaticReconnect(true);
//				connOpts.setWill(Constant.onlineTopic, AESUtil.encrypt(Constant.offlineMsg).getBytes(), Constant.onlineQos, false);
				System.out.println("正在连接broker");
				sampleClient.connect(connOpts);
				System.out.println("连接成功");
				//StatusUI.setStatus("在线");
			} catch (MqttException me) {
				System.out.println("reason "+me.getReasonCode());
				System.out.println("msg "+me.getMessage());
				System.out.println("loc "+me.getLocalizedMessage());
				System.out.println("cause "+me.getCause());
				System.out.println("excep "+me);
				System.out.println("网络无法连接");

				try {
					Thread.sleep(5000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				//StatusUI.setStatus("离线");
				if(true/*i<=5*/) {
					System.out.println("尝试重连第"+(i)+"次");
					i++;
					try {
//						sampleClient = null;
						MqttClientUtil.getInstance();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return sampleClient;
	}

	public static void publishTopic(String topic, int qos, String content, boolean retained) {
		MqttClient sampleClient = MqttClientUtil.getInstance();
		System.out.println("发布主题"+topic);
		System.out.println("正在发布消息"+content);
		
		try {
			MqttMessage message = new MqttMessage(content.getBytes("UTF-8"));
			message.setQos(qos);
			message.setRetained(retained);
			sampleClient.publish(topic, message);
			//StatusUI.setStatus("在线");
		} catch (MqttPersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//StatusUI.setStatus("离线");
		} catch (MqttException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//StatusUI.setStatus("离线");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("消息发布成功  "+sdf.format(System.currentTimeMillis()));
	}

	public static void subscribeTopic(String topic, int qos) {
		MqttClient sampleClient = MqttClientUtil.getInstance();
		System.out.println("订阅主题"+topic);
		try {
			sampleClient.subscribe(topic,qos);
			//StatusUI.setStatus("在线");
		} catch (MqttPersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//StatusUI.setStatus("离线");
		} catch (MqttException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//StatusUI.setStatus("离线");
		}
	}
}
